package com.lhs.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lhs.entity.blog.article_search;
import com.lhs.entity.blog.blog_post;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
* @author 15977
* @description 针对表【blog_post_0(博客文章表（分表）)】的数据库操作Mapper
* @createDate 2025-09-06 01:49:57
* @Entity com.lhs.blog.blog_post_0
*/

@Mapper
public interface blog_post_0Mapper extends BaseMapper<blog_post> {

    //查询博客文章
//    @Select("SELECT id, title, cover, content, user_id, like_count, comment_count, create_time, update_time FROM blog_pos//    Blog_post getAllBlog();t_0")

    //查询所有博客文章(包括不公开)
    @Select("SELECT id, title, cover, content, user_id, like_count,view_count, comment_count, create_time, update_time ,publish_time \n" +
            "    FROM blog_post_0\n" +
            "    ORDER BY create_time DESC")
    IPage<blog_post> selectAllBlog(Page<blog_post> page);

    //查询所有博客文章(仅公开)
    @Select("SELECT a.id, a.title, ANY_VALUE(b.cover) AS cover,GROUP_CONCAT(c.label) AS label, a.content, a.user_id  AS authorId,a.status, a.like_count,a.view_count, a.comment_count, a.update_time ,a.create_time,a.publish_time \n" +
            "    FROM blog_post_0  a \n" +
            "LEFT JOIN blog_cover b on  b.post_id =a.id \n" +
            "LEFT JOIN blog_label c on c.post_id=a.id \n"+
            "WHERE a.status = 1\n "+
            "GROUP BY a.id \n"+
            "ORDER BY a.create_time DESC ")
    List<article_search> getAllListblogs();




    //查询所有博客文章的id（仅公开）
    @Select("SELECT id FROM blog_post_0 WHERE status = 1")
    List<Long> selectAllIds();

    //新增博客文章
    @Select("INSERT INTO blog_post_0 (id, title, cover, content, user_id, like_count, comment_count, create_time, update_time,publish_time) VALUES (#{id}, #{title}, #{cover}, #{content}, #{user_id}, #{like_count}, #{comment_count}, #{create_time}, #{update_time}),#{publish_time}")
    blog_post saveBlog(blog_post blog);

    //查询博客文章
    @Select("SELECT id, title, cover, content, user_id, like_count,view_count, comment_count, create_time, update_time ,publish_time FROM blog_post_0 WHERE id = #{id}")
    blog_post getBlogPostById(Long id);

    //删除博客文章
    @Select("DELETE FROM blog_post_0 WHERE id = #{id}")
    void removeById(Long id);

        //检查用户id和博客id的关系是否存在
    @Select("SELECT user_id, blog_id FROM blog_post_0 WHERE user_id = #{userId} AND blog_id = #{blogId}")
    Boolean checkUserBlogRelationExists(Long userId, Long blogId);

    @Select("UPDATE blog_post_0 SET like_count = like_count - 1 WHERE id = #{postId}")
    void decrementLikeCount(Long postId);

    @Select("UPDATE blog_post_0 SET like_count = like_count + 1 WHERE id = #{postId}")
    void incrementLikeCount(Long postId);


    @Select("SELECT * FROM blog_post_0 WHERE id = #{postId}")
    blog_post selectBlogPostById(Long postId);

    @Update("UPDATE blog_post_0 SET like_count = #{likeCount} WHERE id = #{postId}")
    void updateLikeCount(Long postId, Long likeCount);

    @Update("UPDATE blog_post_0 SET view_count = view_count + 1 WHERE id = #{postId}")
    void updateViewCount(Long postId, Long viewCount);

    @Select("SELECT view_count FROM blog_post_0 WHERE id = #{postId}")
    Long selectViewCountByPostId(Long postId);

    @Update("UPDATE blog_post_0 SET status = #{status} WHERE id = #{id}")
    void updateStatus(Long id, int i);
}




